﻿Imports CacheScope
Module Module1
    Sub Main()
        Console.WriteLine("Start Test")

        'Dim CacheFactory1 As CacheFactory = New CacheFactory()
        'Dim Cache1 As Cache
        'Cache1 = CacheFactory1.GetCache(Nothing)
        'Dim TS As System.TimeSpan = TimeSpan.FromSeconds(30)
        'Dim Region As String = "DefaultRegion"
        'CachingService.CheckandCreateRegion(Region, Cache1)

        'Dim LC1 As New LockHandle
        'Dim LC2 As New LockHandle
        'Console.WriteLine("Add Item 1 - 1")
        'Cache1.Put(Region, "Item 1", "1", TS)
        'Console.WriteLine(Cache1.Get(Region, "Item 1"))

        'Console.WriteLine("Read and Lock Item 1 - 1")
        'Console.WriteLine(Cache1.GetAndLock(Region, "Item 1", TS, LC1))
        'Try
        '    Console.WriteLine(Cache1.GetAndLock(Region, "Item 1", TS, LC1))
        '    Console.WriteLine(Cache1.GetAndLock(Region, "Item 2", TS, LC2))
        'Catch ex As CacheException
        '    Select Case Left(ex.Message, 22)
        '        Case "ErrorCode<ERRCA0008>" 'Key doesn't exist
        '        Case "ErrorCode<ERRCA0013>" 'Key already locked
        '    End Select
        'Catch ex As Exception
        '    Throw New Exception
        'End Try

        'Test1()
        CachingServiceTest()
        'test3()
        'Test2()
        GC.Collect(2, GCCollectionMode.Forced)

        Console.WriteLine("End Test")
        Console.ReadLine()
    End Sub
    Sub test3()
        'Dim M_Cache As New CachingService
        Using CS1 As New CachingScope
            Dim ItemCount As Int32 = 1 * (10 ^ 3)
            Console.WriteLine("Adding Objects in Base scope")
            Console.WriteLine(Now.ToLongTimeString)
            For i As Int32 = 1 To ItemCount
                CachingService.PutCacheItem("Item" & i, "Andrew " & i)
            Next
            Console.WriteLine(Now.ToLongTimeString)

            Console.WriteLine("Reading Objects with no lock outside cachescope")
            Console.WriteLine(Now.ToLongTimeString)
            Dim O2 As Object
            For i As Int32 = 1 To ItemCount
                O2 = CachingService.GetCacheItem("Item" & i)
            Next
            Console.WriteLine(Now.ToLongTimeString)
        End Using
    End Sub
    Sub Test2()

        Dim Ar As New ArrayList
        For i As Int64 = 1 To 1 * (10 ^ 7)
            Ar.Add(i)
        Next

    End Sub
    Sub Test1()
        'Dim ItemCount As Int32 = 1
        Dim ItemCount As Int32 = 5 * (10 ^ 4)

        Console.WriteLine("Adding Objects in Base scope")
        Console.WriteLine(Now.ToLongTimeString)
        For i As Int32 = 1 To ItemCount
            CachingService.PutCacheItem("Item" & i, "Andrew " & i)
        Next
        Console.WriteLine(Now.ToLongTimeString)

        Console.WriteLine("Reading Ojbects with no lock outside cachescope")
        Console.WriteLine(Now.ToLongTimeString)
        Dim O2 As Object
        For i As Int32 = 1 To ItemCount
            'Console.WriteLine(CachingService.GetCacheItem("Item" & i))
            O2 = CachingService.GetCacheItem("Item" & i)
        Next
        Console.WriteLine(Now.ToLongTimeString)



        Console.WriteLine("Adding Objects in Base scope")
        Console.WriteLine(Now.ToLongTimeString)
        For i As Int32 = 1 To ItemCount
            CachingService.PutCacheItem(CachingService.CacheListEnum.Server1, "Item" & i, "Andrew " & i)
        Next
        Console.WriteLine(Now.ToLongTimeString)

        Console.WriteLine("Reading Ojbects with no lock outside cachescope")
        Console.WriteLine(Now.ToLongTimeString)
        For i As Int32 = 1 To ItemCount
            'Console.WriteLine(CachingService.GetCacheItem("Item" & i))
            O2 = CachingService.GetCacheItem(CachingService.CacheListEnum.Server2, "Item" & i)
        Next
        Console.WriteLine(Now.ToLongTimeString)


        'Dim Cache1 As Cache
        'Cache1 = CacheFactory1.GetCache(Nothing)
        'Dim TS As System.TimeSpan = TimeSpan.FromSeconds(30)
        'Dim Region As String = "DefaultRegion"
        'CachingService.CheckandCreateRegion(Region, Cache1)
        'Dim LC1 As New LockHandle
        'Dim LC2 As New LockHandle
        'Console.WriteLine("Add Item 1 - 1")

        'Try
        'Dim CacheFactory1 As CacheFactory = New CacheFactory()
        'For i As Int32 = 1 To 1 * (10 ^ 4)
        '    CacheFactory1 = New CacheFactory()
        '    CacheFactory1 = Nothing
        'Next
        'Catch ex As Exception

        'End Try

        'Dim O2 As Object
        'For i As Int32 = 1 To 1 * (10 ^ 3)
        '    Try
        '        CacheFactory1 = New CacheFactory()
        '        'Cache1 = CacheFactory1.GetCache(Nothing)
        '        'O2 = Cache1.Get(Region, "Item" & i)
        '        'Cache1 = Nothing
        '        CacheFactory1 = Nothing
        '    Catch ex As Exception

        '    End Try
        'Next

        'Console.WriteLine(Cache1.Get(Region, "Item 1"))

        'Console.WriteLine("Add Item 1 - 2")
        'Cache1.Put(Region, "Item 1", "2", TS)

        'Console.WriteLine("Read Item 1 - 2")
        'Console.WriteLine(Cache1.Get(Region, "Item 1"))

        'Console.WriteLine("Read and Lock Item 1 - 2")
        'Console.WriteLine(Cache1.GetAndLock(Region, "Item 1", TS, LC1))

        'Console.WriteLine("Add Item 1 - 3")
        'Cache1.Put(Region, "Item 1", "3", TS)

        'Console.WriteLine("Read Item 1 - 3")
        'Console.WriteLine(Cache1.Get(Region, "Item 1"))

        'Console.WriteLine("Read and Lock Item 1 - 3")
        'Console.WriteLine(Cache1.GetAndLock(Region, "Item 1", TS, LC2))

        'Console.WriteLine("Add and Unlock Item 1 - 4")
        'Cache1.PutAndUnlock(Region, "Item 1", "4", TS, LC2, Nothing)

        'Console.WriteLine("Read Item 1 - 4")
        'Console.WriteLine(Cache1.Get(Region, "Item 1"))
    End Sub
    Sub CachingServiceTest()
        Dim ItemCount As Int32 = 1 * (10 ^ 3)
        Using CS1 As New CachingScope
            'Dim CS1 As New CachingScope
            Console.WriteLine("Adding Objects in Base scope")
            Console.WriteLine(Now.ToLongTimeString)
            For i As Int32 = 1 To ItemCount
                CachingService.PutCacheItem("Item" & i, "Andrew " & i)
            Next
            Console.WriteLine(Now.ToLongTimeString)
            '    Using CS2 As New CachingScope
            '        Console.WriteLine("Adding Objects in child scope")
            '        For i As Int16 = 6 To 10
            '            CachingService.PutCacheItem("Item" & i, "Andrew " & i)
            '        Next
            '    End Using
            '    Using CS3 As New CachingScope(CachingScope.ScopeTypeEnum.RequiresNew)
            '        Console.WriteLine("Adding Objects in New Base Scope")
            '        For i As Int16 = 11 To 15
            '            CachingService.PutCacheItem("Item" & i, "Andrew " & i)
            '        Next
            '        For i As Int16 = 11 To 15
            '            Console.WriteLine(CachingService.GetandLcokCacheItem("Item" & i))
            '        Next
            '    End Using
            '    Console.WriteLine("Reading Ojbects with no lock")
            '    For i As Int16 = 1 To 15
            '        Console.WriteLine(CachingService.GetCacheItem("Item" & i))
            '    Next
            '    Console.WriteLine("Reading Ojbects with lock")
            'For i As Int16 = 1 To 5 * (10 ^ 3)
            'Console.WriteLine(CachingService.GetandLcokCacheItem("Item" & i))
            'Next
            'End Using
            Console.WriteLine("Reading Ojbects with no lock")
            Console.WriteLine(Now.ToLongTimeString)
            Dim O1 As Object
            For i As Int32 = 1 To ItemCount
                'Console.WriteLine(CachingService.GetCacheItem("Item" & i))
                O1 = CachingService.GetCacheItem("Item" & i)
            Next
            Console.WriteLine(Now.ToLongTimeString)
        End Using

        GC.Collect(2, GCCollectionMode.Forced)
        Console.WriteLine("Reading Ojbects with no lock outside cachescope")
        Console.WriteLine(Now.ToLongTimeString)
        Dim O2 As Object
        For i As Int32 = 1 To ItemCount
            'Console.WriteLine(CachingService.GetCacheItem("Item" & i))
            O2 = CachingService.GetCacheItem("Item" & i)
        Next
        Console.WriteLine(Now.ToLongTimeString)
    End Sub
End Module
